import os
import webapp2
import mimetypes
import hashlib

class FileHandler(webapp2.RequestHandler):
    root_path = None

    def get(self, path):
        abs_path = os.path.abspath(os.path.join(self.app.config.get('forest_root'), path))
        if os.path.isdir(abs_path):
            self.response.set_status(403)
            return
        try:
            with open(abs_path, 'r') as f:
                f_content = f.read()
            self.response.headers.add_header('Content-Type', mimetypes.guess_type(abs_path)[0])
            self.response.headers.add_header('Content-MD5', hashlib.md5(f_content).hexdigest())
            self.response.out.write(f_content)

        except Exception, e:
            print "error", e
            self.response.set_status(404)
